//
//  CLLocationCoordinate2D+Ext.swift
//  ANOASTORE
//
//  Created by hudan on 2025/11/24.
//  Copyright © 2025 zwb. All rights reserved.
//

import Foundation
import CoreLocation

extension CLLocationCoordinate2D {
    static func isSameLocation(loction1: CLLocationCoordinate2D, location2: CLLocationCoordinate2D) -> Bool {
        let latitude1 = "\(loction1.latitude)"
        let longitude1 = "\(loction1.longitude)"
        
        let latitudePointRange = latitude1.rangeOf(str: ".")
        let longitudePointRange = longitude1.rangeOf(str: ".")
        
        if latitudePointRange.location > 0 && longitudePointRange.location > 0 {
            
            let latitude2 = "\(location2.latitude)"
            let longitude2 = "\(location2.longitude)"
            
            var subLatLength = 0
            var subLongLength = 0
            
            if latitude1.length >= latitudePointRange.location + 4 {
                subLatLength = latitudePointRange.location + 4
            } else {
                subLatLength = latitude1.length
            }
            
            if longitude1.length >= longitudePointRange.location + 4 {
                subLongLength = longitudePointRange.location + 4
            } else {
                subLongLength = longitude1.length
            }
            
            if latitude2.length < subLatLength {
                subLatLength = latitude2.length
            }
            if longitude2.length < subLongLength {
                subLongLength = longitude2.length
            }
            
            if subLatLength > 0 && subLongLength > 0 {
                let cmpLat1 = latitude1.subTo(subLatLength)
                let cmpLong1 = longitude1.subTo(subLongLength)
                
                let cmpLat2 = latitude2.subTo(subLatLength)
                let cmpLong2 = longitude2.subTo(subLongLength)
                
                return (cmpLat1 == cmpLat2) && (cmpLong1 == cmpLong2)
            } else {
                return false
            }
        }
        
        return false
    }
    
}
